/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package service;

import entities.ProjColumn;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.DELETE;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.QueryParam;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.WebApplicationException;
import javax.persistence.NoResultException;
import javax.persistence.EntityManager;
import entities.Project;
import converter.ProjColumnConverter;
import java.util.Hashtable;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author Alex
 */

@Stateless
public class ProjColumnResource {

    @javax.ejb.EJB
    private ProjIdResourceSub projIdResourceSub;

    @Context
    protected UriInfo uriInfo;

    protected EntityManager em;
    protected Long id;

    /** Creates a new instance of ProjColumnResource */
    public ProjColumnResource() {
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setEm(EntityManager em) {
        this.em = em;
    }

    /**
     * Get method for retrieving an instance of ProjColumn identified by id in XML format.
     *
     * @param id identifier for the entity
     * @return an instance of ProjColumnConverter
     */
    @GET
    @Produces({"application/xml", "application/json"})
    public ProjColumnConverter get(@QueryParam("expandLevel")
                                   @DefaultValue("1") int expandLevel) {
        return new ProjColumnConverter(getEntity(), uriInfo.getAbsolutePath(), expandLevel);
    }

    /**
     * Put method for updating an instance of ProjColumn identified by id using XML as the input format.
     *
     * @param id identifier for the entity
     * @param data an ProjColumnConverter entity that is deserialized from a XML stream
     */
    @PUT
    @Consumes({"application/xml", "application/json"})
    public void put(ProjColumnConverter data) {
        updateEntity(getEntity(), data.resolveEntity(em));
    }

    /**
     * Delete method for deleting an instance of ProjColumn identified by id.
     *
     * @param id identifier for the entity
     */
    @DELETE
    public void delete() {
        deleteEntity(getEntity());
    }

    /**
     * Returns an instance of ProjColumn identified by id.
     *
     * @param id identifier for the entity
     * @return an instance of ProjColumn
     */
    protected ProjColumn getEntity() {
        try {
            return (ProjColumn) em.createQuery("SELECT e FROM ProjColumn e where e.id = :id").setParameter("id", id).getSingleResult();
        } catch (NoResultException ex) {
            throw new WebApplicationException(new Throwable("Resource for " + uriInfo.getAbsolutePath() + " does not exist."), 404);
        }
    }

    /**
     * Updates entity using data from newEntity.
     *
     * @param entity the entity to update
     * @param newEntity the entity containing the new data
     * @return the updated entity
     */
    private ProjColumn updateEntity(ProjColumn entity, ProjColumn newEntity) {
        Project projId = entity.getProjId();
        Project projIdNew = newEntity.getProjId();
        entity = em.merge(newEntity);
        if (projId != null && !projId.equals(projIdNew)) {
            projId.getProjColumnList().remove(entity);
        }
        if (projIdNew != null && !projIdNew.equals(projId)) {
            projIdNew.getProjColumnList().add(entity);
        }
        return entity;
    }

    /**
     * Deletes the entity.
     *
     * @param entity the entity to deletle
     */
    private void deleteEntity(ProjColumn entity) {
        Project projId = entity.getProjId();
        if (projId != null) {
            projId.getProjColumnList().remove(entity);
        }
        em.remove(entity);
    }

    /**
     * Returns a dynamic instance of ProjectResource used for entity navigation.
     *
     * @param id identifier for the parent entity
     * @return an instance of ProjectResource
     */
    @Path("projId/")
    public ProjectResource getProjIdResource() {
        projIdResourceSub.setParent(getEntity());
        return projIdResourceSub;
    }

    @Stateless
    public static class ProjIdResourceSub extends ProjectResource {

        private ProjColumn parent;

        public void setParent(ProjColumn parent) {
            this.parent = parent;
        }

        @Override
        protected Project getEntity() {
            Project entity = parent.getProjId();
            if (entity == null) {
                throw new WebApplicationException(new Throwable("Resource for " + uriInfo.getAbsolutePath() + " does not exist."), 404);
            }
            return entity;
        }
    }
}
